﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebAppAuthAdmin.AuthApiManagementServiceReference;
using Web.Framework;
using AuthApi.Contracts;
using Web.Framework.Svc;
using WebAppAuthAdmin.AuthApiServiceReference;
using AuthApi.Contracts.Search;
using System.Web.UI.WebControls;
using WebAppAuthAdmin.Code.Security;

namespace WebAppAuthAdmin.Code
{
    public class WebServiceHelper
    {
        public ServiceOptions ServiceOptionsDefault
        {
            get;
            set;
        }


        public static bool Authenticate(int applicationId, int companyId, string userName, string password)
        {
            AuthApiClient client = new AuthApiClient();
            bool isAuthenticated = false;
            try
            {
                isAuthenticated = client.ValidateUser(applicationId, companyId, userName, password);
                client.Close();
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }

            return isAuthenticated;
        }

        public static List<Company> GetCompanies(int applicationId)
        {
            AuthApiClient client = new AuthApiClient();
            List<Company> companies = new List<Company>();

            try
            {
                companies.AddRange(client.GetCompanies(applicationId));
                client.Close();

            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }

            return companies;
        }

        public bool ResetPassword(string username)
        {
            AuthApiClient client = new AuthApiClient();            

            try
            {
                var t = client.ResetPassword(username);
                client.Close();
                return t;

            }
            catch (Exception ex)
            {
                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }            
        }

        public bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            AuthApiClient client = new AuthApiClient();            

            try
            {
                var t = client.ChangePassword(username, oldPassword, newPassword);
                client.Close();
                return t;

            }
            catch (Exception ex)
            {
                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }            
        }

        public static List<AdUser> GetUsers(int applicationId, int companyId, string userName)
        {
            AuthApiClient client = new AuthApiClient();
            List<AdUser> usuarios = new List<AdUser>();
            try
            {
                usuarios.AddRange(client.GetUsers(applicationId, companyId, userName));
                client.Close();
            }
            catch (Exception ex)
            {

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }

            return usuarios;
        }

        public static List<string> GetOperationsForUser(int applicationId, int companyId, string userName)
        {
            AuthApiClient client = new AuthApiClient();
            List<string> operations = new List<string>();

            try
            {
                operations.AddRange(client.GetOperationsForUser(applicationId, companyId, userName));
                client.Close();
            }
            catch (Exception ex)
            {


                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Abort();
                }
                throw ex;
            }

            return operations;
        }

        public static List<WebApplicationInfo> GetApplicationList(string nameLike)
        {
            List<WebApplicationInfo> applicationList = new List<WebApplicationInfo>();
            AuthApiManagementClient mgmSrv = new AuthApiManagementClient();
            try
            {
                applicationList.AddRange(mgmSrv.GetApplications(nameLike));
                mgmSrv.Close();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);

                if (mgmSrv.State != System.ServiceModel.CommunicationState.Closed)
                {
                    mgmSrv.Abort();
                }
                throw ex;
            }

            return applicationList;
        }

        public static WebApplicationInfo GetApplicationById(int appId)
        {
            WebApplicationInfo application = null;
            AuthApiManagementClient mgmSrv = new AuthApiManagementClient();
            try
            {
                application = mgmSrv.GetApplicationById(appId);
                mgmSrv.Close();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);

                if (mgmSrv.State != System.ServiceModel.CommunicationState.Closed)
                {
                    mgmSrv.Abort();
                }
                throw ex;
            }

            return application;
        }

        public static void DeleteUser(string userName)
        {            
            AuthApiManagementClient mgmSrv = new AuthApiManagementClient();
            try
            {
                mgmSrv.DeleteUser(userName);
                mgmSrv.Close();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);

                if (mgmSrv.State != System.ServiceModel.CommunicationState.Closed)
                {
                    mgmSrv.Abort();
                }
                throw ex;
            }            
        }

        public static void DeleteApplicationById(int appId)
        {            
            AuthApiManagementClient mgmSrv = new AuthApiManagementClient();
            try
            {
                mgmSrv.DeleteApplication(appId);
                mgmSrv.Close();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);

                if (mgmSrv.State != System.ServiceModel.CommunicationState.Closed)
                {
                    mgmSrv.Abort();
                }
                throw ex;
            }        
        }      

        public static WebApplicationInfo SaveApplication(WebApplicationInfo app)
        {
            WebApplicationInfo application = null;
            AuthApiManagementClient mgmSrv = new AuthApiManagementClient();
            try
            {
                application = mgmSrv.SaveApplication(app);
                mgmSrv.Close();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);

                if (mgmSrv.State != System.ServiceModel.CommunicationState.Closed)
                {
                    mgmSrv.Abort();
                }
                throw ex;
            }

            return application;
        }

        public UserWithApplication GetUserWithApplications(int companyId, string userName)
        {
            AuthApiManagementClient mgmSrv = new AuthApiManagementClient();
            UserWithApplication userWithApp = new UserWithApplication();
            try
            {
                userWithApp = mgmSrv.GetUserWithApplications(companyId, userName);
                mgmSrv.Close();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);

                if (mgmSrv.State != System.ServiceModel.CommunicationState.Closed)
                {
                    mgmSrv.Abort();
                }
                throw ex;
            }

            return userWithApp;
        }

        public static List<AdUser> GetUsers(SearchUser search)
        {
            AuthApiManagementClient mgmSrv = new AuthApiManagementClient();
            List<AdUser> users = new List<AdUser>();
            try
            {
                users.AddRange(mgmSrv.GetUsers(search));
                mgmSrv.Close();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);

                if (mgmSrv.State != System.ServiceModel.CommunicationState.Closed)
                {
                    mgmSrv.Abort();
                }
                throw ex;
            }

            return users;
        }

        public static AdUser GetUser(int applicationId, int companyId, string userName)
        {
            AuthApiManagementClient mgmSrv = new AuthApiManagementClient();
            AdUser user = null;
            try
            {
                user = mgmSrv.GetUser(applicationId, companyId, userName);
                mgmSrv.Close();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);

                if (mgmSrv.State != System.ServiceModel.CommunicationState.Closed)
                {
                    mgmSrv.Abort();
                }
                throw ex;
            }

            return user;
        }

        public static AdUser SaveUser(AdUser user)
        {            
            AuthApiManagementClient mgmSrv = new AuthApiManagementClient();
            try
            {
                user = mgmSrv.SaveUser(user);
                mgmSrv.Close();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);

                if (mgmSrv.State != System.ServiceModel.CommunicationState.Closed)
                {
                    mgmSrv.Abort();
                }
                throw ex;
            }

            return user;
        }        
       
        public static List<Company> GetCompanyList(string nameLike)
        {
            List<Company> companyList = new List<Company>();
            AuthApiManagementClient mgmSrv = new AuthApiManagementClient();
            try
            {
                companyList.AddRange(mgmSrv.GetCompanies(nameLike));
                mgmSrv.Close();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);

                if (mgmSrv.State != System.ServiceModel.CommunicationState.Closed)
                {
                    mgmSrv.Abort();
                }
                throw ex;
            }

            return companyList;
        }

        public static Company GetCompanyById(int companyId)
        {
            Company company = null;
            AuthApiManagementClient mgmSrv = new AuthApiManagementClient();
            try
            {
                company = mgmSrv.GetCompanyWithApplicationsById(companyId);
                mgmSrv.Close();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);

                if (mgmSrv.State != System.ServiceModel.CommunicationState.Closed)
                {
                    mgmSrv.Abort();
                }
                throw ex;
            }

            return company;
        } 

        public static void DeleteCompanyById(int companyId)
        {           
            AuthApiManagementClient mgmSrv = new AuthApiManagementClient();
            try
            {
                mgmSrv.DeleteCompany(companyId);
                mgmSrv.Close();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);

                if (mgmSrv.State != System.ServiceModel.CommunicationState.Closed)
                {
                    mgmSrv.Abort();
                }
                throw ex;
            }            
        }

        public static void DeleteRole(int applicationId, int roleId)
        {
            AuthApiManagementClient mgmSrv = new AuthApiManagementClient();
            try
            {
                mgmSrv.DeleteRoleForApplication(applicationId, roleId);
                mgmSrv.Close();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);

                if (mgmSrv.State != System.ServiceModel.CommunicationState.Closed)
                {
                    mgmSrv.Abort();
                }
                throw ex;
            }
        }

        public static Company SaveCompany(Company company)
        {
            Company retValCompany = null;
            AuthApiManagementClient mgmSrv = new AuthApiManagementClient();
            try
            {
                retValCompany = mgmSrv.SaveCompany(company);
                mgmSrv.Close();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);

                if (mgmSrv.State != System.ServiceModel.CommunicationState.Closed)
                {
                    mgmSrv.Abort();
                }
                throw ex;
            }

            return retValCompany;
        }

        public static void DeleteOperation(int operationId)
        {
            AuthApiManagementClient mgmSrv = new AuthApiManagementClient();
            try
            {
                mgmSrv.DeleteOperation(operationId);
                mgmSrv.Close();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);

                if (mgmSrv.State != System.ServiceModel.CommunicationState.Closed)
                {
                    mgmSrv.Abort();
                }
                throw ex;
            }
        }

        public static RoleIncluded GetRoleForApplicationById(int applicationId, int roleId)
        {
            AuthApiManagementClient mgmSrv = new AuthApiManagementClient();
            try
            {
                var t= mgmSrv.GetRoleForApplicationById(applicationId, roleId);
                mgmSrv.Close();
                return t;
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);

                if (mgmSrv.State != System.ServiceModel.CommunicationState.Closed)
                {
                    mgmSrv.Abort();
                }
                throw ex;
            }
        }

        public static List<OperationIncluded> GetOperationsForApplication(int applicationId)
        {
            AuthApiManagementClient mgmSrv = new AuthApiManagementClient();
            try
            {
                var t= mgmSrv.GetOperationsForApplication(applicationId);
                mgmSrv.Close();
                return t;
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);

                if (mgmSrv.State != System.ServiceModel.CommunicationState.Closed)
                {
                    mgmSrv.Abort();
                }
                throw ex;
            }
        }

        public static RoleIncluded SaveRole(RoleIncluded role)
        {
            AuthApiManagementClient mgmSrv = new AuthApiManagementClient();
            try
            {
                var t = mgmSrv.SaveRole(role);
                mgmSrv.Close();
                return t;
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);

                if (mgmSrv.State != System.ServiceModel.CommunicationState.Closed)
                {
                    mgmSrv.Abort();
                }

                throw ex;
            }
        }

        public static OperationIncluded GetOperation(SearchOperation search)
        {
            AuthApiManagementClient mgmSrv = new AuthApiManagementClient();
            try
            {
                var t = mgmSrv.GetOperation(search);
                mgmSrv.Close();
                return t;
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);

                if (mgmSrv.State != System.ServiceModel.CommunicationState.Closed)
                {
                    mgmSrv.Abort();
                }

                throw ex;
            }
        }

        public static OperationIncluded GetOperationById(int operationId)
        {
            AuthApiManagementClient mgmSrv = new AuthApiManagementClient();
            try
            {
                var t = mgmSrv.GetOperationById(operationId);
                mgmSrv.Close();
                return t;
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);

                if (mgmSrv.State != System.ServiceModel.CommunicationState.Closed)
                {
                    mgmSrv.Abort();
                }

                throw ex;
            }
        }

        public static OperationIncluded SaveOperation(OperationIncluded operation)
        {
            AuthApiManagementClient mgmSrv = new AuthApiManagementClient();
            try
            {
                var t=  mgmSrv.SaveOperation(operation);
                mgmSrv.Close();
                return t;
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);

                if (mgmSrv.State != System.ServiceModel.CommunicationState.Closed)
                {
                    mgmSrv.Abort();
                }

                throw ex;
            }
        }

        public List<RoleIncluded> GetRolesForApplication(int applicationId)
        {
            AuthApiManagementClient mgmSrv = new AuthApiManagementClient();
            try
            {
                var t = mgmSrv.GetRolesForApplication(applicationId);
                mgmSrv.Close();
                return t;
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);

                if (mgmSrv.State != System.ServiceModel.CommunicationState.Closed)
                {
                    mgmSrv.Abort();
                }

                throw ex;
            }
        }        

        public static UserWithApplication CreateUser(UserWithApplication user)
        {
            AuthApiManagementClient mgmSrv = new AuthApiManagementClient();
            try
            {
                var t = mgmSrv.CreateUser(user);
                mgmSrv.Close();
                return t;
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);

                if (mgmSrv.State != System.ServiceModel.CommunicationState.Closed)
                {
                    mgmSrv.Abort();
                }

                throw ex;
            }
        }

        public void IncluirUsuarioEnAplicacion(int companyId, int applicationId, string userName)
        {
            AuthApiManagementClient mgmSrv = new AuthApiManagementClient();
            try
            {
                mgmSrv.IncluirUsuarioEnAplicacion(companyId, applicationId, userName);
                mgmSrv.Close();                
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);

                if (mgmSrv.State != System.ServiceModel.CommunicationState.Closed)
                {
                    mgmSrv.Abort();
                }

                throw ex;
            }
        }

        public void BloquearUsuario(int companyId, int applicationId, string userName, bool bloquear)
        {
            AuthApiManagementClient mgmSrv = new AuthApiManagementClient();
            try
            {
                mgmSrv.BloquearUsuario(companyId, applicationId, userName, bloquear);
                mgmSrv.Close();               
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);

                if (mgmSrv.State != System.ServiceModel.CommunicationState.Closed)
                {
                    mgmSrv.Abort();
                }

                throw ex;
            }
        }

        public bool ExistCompanyApplication(int companyId, int applicationId)
        {
            AuthApiManagementClient mgmSrv = new AuthApiManagementClient();
            try
            {
                var t = mgmSrv.ExistCompanyApplication(companyId, applicationId);
                mgmSrv.Close();
                return t;
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);

                if (mgmSrv.State != System.ServiceModel.CommunicationState.Closed)
                {
                    mgmSrv.Abort();
                }

                throw ex;
            }
        }

        public AdUser SaveAppUser(AdUser adUser)
        {
            AuthApiManagementClient mgmSrv = new AuthApiManagementClient();
            try
            {
                var t = mgmSrv.SaveAppUser(adUser);
                mgmSrv.Close();
                return t;
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);

                if (mgmSrv.State != System.ServiceModel.CommunicationState.Closed)
                {
                    mgmSrv.Abort();
                }

                throw ex;
            }
        }
    }
}